package com.example.farmeri.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.farmeri.R;
import com.example.farmeri.http.API;
import com.example.farmeri.data.DataParam;
import com.example.farmeri.data.RetrofitManager;
import com.example.farmeri.pojo.Farm;
import com.example.farmeri.tools.ResponseUtils;
import com.example.farmeri.ui.farm_data.FarmDataLookActivity;

import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

/**
 * Create by weiqishan on 2020/4/6.
 * 配置适配器
 */
public class FarmDataAdapter extends BaseRecycleViewAdapter<Farm>{

    Context context;
    Integer layout;

    public FarmDataAdapter(Context context, int layout) {
        super(context, layout);
        this.context = context;
        this.layout = layout;
        fetchData(1,99);
    }


    public void fetchData(Integer page, Integer limit) {
        RetrofitManager
                .getManager()
                .getRetrofit()
                .create(API.class)
                .getAllFarmsByUser(
                        DataParam.getUserInfo().getId(),
                        page.toString(),
                        limit.toString())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseUtils<List<Farm>>>() {
                    @Override
                    public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@io.reactivex.rxjava3.annotations.NonNull ResponseUtils<List<Farm>> listResponseUtils) {
                        if (listResponseUtils.getCode() == 200){
                            List<Farm> farmList = listResponseUtils.getData();
                            Integer position = dataList.size();
                            dataList.addAll(farmList);
                            notifyItemInserted(position);
                        }else {
                            showMsg(listResponseUtils.getMessage());
                        }
                    }

                    @Override
                    public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                        Log.e(TAG, "onError: ");
                        showError();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View itemView = inflater.inflate(layout,viewGroup,false);
        Log.d("TAG", "onCreateViewHolder: ");
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        Log.d("TAG", "onBindViewHolder: ");
        MyViewHolder myViewHolder = (MyViewHolder) viewHolder;
        Farm farm = dataList.get(i);
        myViewHolder.farm.setText(farm.getName());
        myViewHolder.address.setText(farm.getLocation());
        myViewHolder.area.setText(farm.getSize());
        myViewHolder.info.setText(farm.getRemarks());
        myViewHolder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, FarmDataLookActivity.class);
                intent.putExtra("farm",farm);
                intent.putExtra("t",farm.getTemperature());
                intent.putExtra("h",farm.getHumidity());
                intent.putExtra("i",farm.getIllumination());
                intent.putExtra("name",farm.getName());
                intent.putExtra("address",farm.getLocation());
                intent.putExtra("id",farm.getId());
                context.startActivity(intent);
            }
        });

    }



    private class MyViewHolder extends RecyclerView.ViewHolder {

        TextView farm;  //田名
        TextView address;   //地址
        TextView area;  //面积
        TextView info;  //备注
        //TextView look;  //查看
        //TextView detail;  //详情
        ImageView pic;
        ViewGroup root; //根布局

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            farm = itemView.findViewById(R.id.item_farm_data_farm);
            address = itemView.findViewById(R.id.item_farm_data_address);
            area = itemView.findViewById(R.id.item_farm_data_area);
            info = itemView.findViewById(R.id.item_farm_data_info);
            //look = itemView.findViewById(R.id.item_farm_data_look);
            //detail = itemView.findViewById(R.id.item_farm_data_detail);
            pic = itemView.findViewById(R.id.item_farm_data_pic);
            root = itemView.findViewById(R.id.item_farm_data_root);

        }
    }
}